\section{Using control variates to estimate the double exponential distribution}
In this problem we seek to estimate the mean of the double exponential distribution, which has the distribution function
\begin{align}
F\paren{x} = \exp\paren{-\exp\paren{-x}}, x\in\mathbb{R}.
\end{align}
To this end we will use a control variate which is exponentially distributed with parameter one. We will simulate values from both distribution using the inverse transform method.
Note that the inverse of the double expontial distribution function is
\begin{align}
F^{-1}\paren{u} = -\log\paren{-\log u}, u\in(0,1)
\end{align}
and that the inverse of the exponential distribution function with parameter one is
\begin{align}
F^{-1}\paren{u} = -\log\paren{1-u}, u\in[0,1)
\end{align}
In algorithm~\vref{Problem5Algo} we apply the inverse transform method to obtain an estimate of the mean.
\begin{algorithm}
\caption{Generate an estimate of double exponential distribution $F\paren{x} = \exp\paren{-\exp\paren{-x}}$}
\label{Problem5Algo}
\begin{algorithmic}[1]
	\REQUIRE Number of the samples to calculate $\hat{b}$ and $s\paren{b}$, $M$ and number of samples to generate $N$.
	\ENSURE $\hat\mu$, an estimate of the mean of the double exponential distribution.
  \STATE $\hat\mu \leftarrow 0$
  \STATE $\bar{Y} \leftarrow 0$
  \STATE $\bar{X} \leftarrow 0$
  \STATE \COMMENT{Pre-run to calculate an estimate of $b$, $\hat{b}$.}
  \FOR{$n \leftarrow 1$ to $M$}
  	\STATE $u \leftarrow Unif\paren{0,1}$
  	\STATE $Y_n \leftarrow -\log\paren{-\log u}$
		\STATE $Y_n \leftarrow -\log\paren{1-u}$
  	\STATE $\bar{X} \leftarrow \bar{X} + X_n$
  	\STATE $\bar{Y} \leftarrow \bar{Y} + Y_n$
  \ENDFOR

	\STATE $\bar{X} \leftarrow \bar{X}/M$
	\STATE $\bar{Y} \leftarrow \bar{Y}/M$
	\STATE $\hat{b} \leftarrow \frac{\sum_{i=1}^M\paren{X_i - \bar{X}}\paren{Y_i - \bar{Y}}}{\sum_{i=1}^M\paren{X_i - \bar{X}}^2}$  
	\STATE \COMMENT{Reset $\bar{Y}$ and $\bar{X}$.}
  \STATE $\bar{Y} \leftarrow 0$
  \STATE $\bar{X} \leftarrow 0$
  \FOR{$n \leftarrow 1$ to $N$}
  	\STATE $u \leftarrow Unif\paren{0,1}$
  	\STATE $Y_n \leftarrow -\log\paren{-\log u}$
		\STATE $Y_n \leftarrow -\log\paren{1-u}$
  	\STATE $\bar{X} \leftarrow \bar{X} + X_n$
  	\STATE $\bar{Y} \leftarrow \bar{Y} + Y_n$
  \ENDFOR
  \STATE $\bar{X} \leftarrow \bar{X}/N$
	\STATE $\bar{Y} \leftarrow \bar{Y}/N$
  \STATE $\bar{Y}(\hat{b}) \leftarrow \bar{Y} - \hat{b}\paren{\bar{X} - 1}$\COMMENT{Since mean of the exponential distribution with parameter one is one.}
  \STATE $s(\hat{b}) \leftarrow \sqrt{\frac{1}{N-1}\sum_{i=1}^{N}\paren{Y_i - \bar{Y}(\hat{b})}^2}$
  \STATE $\epsilon \leftarrow 1.96\frac{s\paren{\hat{b}}}{\sqrt{N}}$
  \RETURN $\bar{Y}(\hat{b}),\epsilon$
\end{algorithmic}
\end{algorithm}
An application of the implementation, given appendix \ref{code5}, of this algorithm gives the following result
\begin{verbatim}
> mean_controlvariates(sample)
[1] 0.579
> error_controlvariates(sample)
[1] 0.0528
\end{verbatim}
while we obtain the following result for a standard application of the inverse tranform method
\begin{verbatim}
> mean_no_controlvariates(sample)
[1] 0.53
> error_no_controlvariates(sample)
[1] 0.245
\end{verbatim}
The error for the latter result is much larger than that of the method using control variates, which is in line with our expectation of the method.
Indeed, the idea of applying control variates is to reduce the variance of the estimate produced.
\newpage
